<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <th:block th:include="common/head::head"></th:block>
    <link th:href="@{${session.jpomProxyPath}+'/static/ztree/zTreeStyle/zTreeStyle.css'}" rel="stylesheet">
    <style>
        body {
            padding: 10px 10px 0px 10px;
        }

        .div-uploadfiles {
            display: none;
            width: 100%;
            margin: 10px 0;
        }

        .div-uploadfiles .layui-inline {
            width: calc(50% - 5px);
            margin-right: 0px;
        }

        .ztree {
            min-height: 80vh;
            overflow-x: scroll;
            padding: 10px;
        }
    </style>
</head>

<body>
<div class="layui-row">
    <button id="uploadFile" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe67c;</i>批量上传</button>

    <button id="uploadFile_Import" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe67c;</i>导入文件</button>

    <button id="uploadFile_Import_dom" class="layui-hide"></button>

    <button id="clear" class="layui-btn layui-btn-sm layui-btn-danger">清空文件</button>


    <div class="layui-layout-right" style="padding-right: 20px;padding-top: 10px;">
        <div>
            文件位置:[[${absLib}]]
        </div>
    </div>
</div>
<div class="layui-row">
    <div class="layui-col-md3">
        <div class="layui-text" style="padding-top: 20px;">当前操作子级：<span id="currentPath"></span></div>
        <ul id="tree" class="ztree"></ul>
    </div>
    <div class="layui-col-md9">
        <table class="layui-table" id="tab_file" lay-filter="tab_file" style="margin: 0;"></table>
    </div>
</div>
</body>

<script type="text/html" id="bar_projects">
    <a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
    {{# if(!d.isDirectory){ }}
    <a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="download">下载</a>
    {{# } }}
</script>
<script type="text/javascript">
    var zTreeObj;
    const setting = {
        data: {
            key: {
                name: "filename",
                children: "children",
                isParent: "isDirectory"
            },
            view: {
                selectedMulti: false
            }
        },
        callback: {
            onClick: zTreeOnExpand,
            onExpand: zTreeOnExpand
        }
    };

    const tableWhre = {
        id: '[[${id}]]'
    };

    var curenntNode = {
        filename: '根路径',
        index: -1,
        isDirectory: true
    };

    function loadSuccess() {
        //
        asyncLoadJs("[[${session.jpomProxyPath}]]/static/ztree/jquery.ztree.core.min.js", function () {
            zTreeObj = $.fn.zTree.init($("#tree"), setting);
            zTreeObj.addNodes(null, curenntNode, false);
            curenntNode = zTreeObj.getNodeByTId("tree_1");
            //
            tableRender({
                id: 'table_file',
                elem: '#tab_file',
                url: './getFileList',
                where: tableWhre,
                cols: [
                    {field: 'index', title: '编号', width: '6%'},
                    {
                        field: 'filename', title: '文件名称', sort: true, width: '30%', templet: function (d) {
                            if (d.isDirectory) {
                                return d.filename + '【文件夹】';
                            }
                            return d.filename;
                        }
                    },
                    {field: 'modifyTime', title: '修改时间', sort: true, width: '20%'},
                    {field: 'fileSize', title: '文件大小', sort: true, width: '15%'},
                    {field: 'op', title: '操作', toolbar: '#bar_projects'}
                ],
                done: function (ret) {
                    var child = ret.data;
                    if (child) {
                        zTreeObj.removeChildNodes(curenntNode);
                        zTreeObj.addNodes(curenntNode, child, false);
                    }
                }
            });
        });

        //
        var updateFail = false;
        var listUploadCount, nowUploadCount;
        uploadRender({
            elem: '#uploadFile',
            accept: 'file',
            data: {
                id: '[[${id}]]',
                levelName: function () {
                    return $("#currentPath").text();
                }
            },
            multiple: true,
            url: './upload',
            choose: function (obj) {
                nowUploadCount = listUploadCount = 0;
                obj.preview(function (index, file, result) {
                    listUploadCount++;
                });
            },
            allDone: function (obj) {
                if (updateFail) {
                    return;
                }
                layer.msg("文件总数：" + obj.total + " 成功上传：" + nowUploadCount + " 上传失败：" + obj.aborted);
                setTimeout(function () {
                    reloadTable();
                }, 2000);
            },
            done: function (res, index, upload) {
                if (res.code !== 200) {
                    layer.msg(res.msg);
                } else {
                }
                // 没有权限
                if (res.code === 302) {
                    updateFail = true;
                }
            }
        });
        $("#uploadFile_Import").click(function () {
            var that = $(this);
            layer.confirm('请选择覆盖方式', {
                btn: ['清空覆盖', '不清空']
            }, function (i) {
                layer.close(i);
                that.attr("type", "clear");
                $("#uploadFile_Import_dom").click();
            }, function () {
                that.attr("type", "noClear");
                $("#uploadFile_Import_dom").click();
            });
        });
        uploadRender({
            elem: '#uploadFile_Import_dom',
            accept: 'file',
            data: {
                id: '[[${id}]]',
                type: "unzip",
                clearType: function () {
                    return $("#uploadFile_Import").attr("type");
                },
                levelName: function () {
                    return $("#currentPath").text();
                }
            },
            multiple: false,
            exts: 'tar|bz2|gz|zip|tar.bz2|tar.gz',
            acceptMime: '.tar,.bz2,.gz,.zip,.tar.bz2,.tar.gz',
            url: './upload',
            done: function (res, index, upload) {
                layer.msg(res.msg);
                setTimeout(function () {
                    reloadTable();
                }, 2000);
            }
        });

        //
        $('#clear').on('click', function () {
            layer.confirm('确定清空此项目文件吗(包括子目录)？', {
                title: '系统提示'
            }, function (index) {
                layer.close(index);
                loadingAjax({
                    url: './deleteFile',
                    data: {
                        id: '[[${id}]]',
                        type: 'clear'
                    },
                    success: function (data) {
                        layer.msg(data.msg);
                        if (200 === data.code) {
                            setTimeout(function () {
                                location.reload();
                            }, 1500);
                        }
                    }
                });
            });
        });

        // 表格工具条事件
        table.on('tool(tab_file)', function (obj) {
            var data = obj.data;
            var event = obj.event;
            if ('delete' === event) {
                // 删除文件
                deleteFile(data);
            } else if ('download' === event) {
                // 下载文件
                downloadFile(data);
            }
        });
    }

    //节点展开
    function zTreeOnExpand(event, treeId, treeNode) {
        if (treeNode.isDirectory) {
            curenntNode = treeNode;
            var filePath = "";
            if (treeNode.index == -1) {
                filePath = "";
            } else {
                filePath = (treeNode.levelName || "") + "/" + treeNode.filename;
            }
            $("#currentPath").text(filePath);
            tableWhre.path = filePath;
            reloadTable();
        }
    }

    function reloadTable() {
        // 刷新项目列表
        table.reload('table_file', {
            url: './getFileList',
            where: tableWhre
        });
    }

    //下载文件
    function downloadFile(data) {
        var url = "./download?id=[[${id}]]&filename=" + encodeURI(data.filename) + "&levelName=" + data.levelName;
        url = appendNodeId(url);
        self.location.href = url;
    }

    // 删除文件
    function deleteFile(data) {
        var msg;
        if (data.isDirectory) {
            msg = '确定删除文件夹 ' + data.filename + '？'
        } else {
            msg = '确定删除文件 ' + data.filename + '？'
        }
        layer.confirm(msg, {
            title: '系统提示'
        }, function (index) {
            layer.close(index);
            loadingAjax({
                url: './deleteFile',
                data: {
                    id: '[[${id}]]',
                    filename: data.filename,
                    levelName: data.levelName
                },
                success: function (data) {
                    layer.msg(data.msg);
                    if (200 === data.code) {
                        reloadTable();
                    }
                }
            });
        });
    }
</script>
</html>
